pub mod on_vector {

    pub fn use_arr() {
        let a = [1, 2, 3, 4, 5];
        for i in &a {
            println!("{i}");
        }

        let months = ["January", "February", "March", "April", "May"];

        for mm in &months {
            println!("{mm}");
        }
    }

    pub fn use_vec() {
        let v: Vec<i32> = Vec::new();
        let v = vec![100, 32, 57];

        let third = &v[2];
        println!("The third element is {third}");
        for i in &v {
            println!("{i}");
        }
    }

    pub fn use_vec_mut() {
        let mut v = vec![1, 2, 3, 4, 5];
        v.push(6);

        let mut v = vec![100, 32, 57];
        for i in &mut v {
            *i += 50;
            println!("{i}");
        }
    }
}

pub mod ab_string {
    pub fn base() {
        let s1 = String::from("hello");
        let s2 = String::from(" world");
        let s3 = s1 + &s2; // note s1 has been moved here and can no longer be used

        // string 的本质是 Vec<u8>, 由于字符编码的不确定, 不能取索引
        // let letter = s2[3]; // error
        let s = &s2[0..4]; // 正确的取索引 但可能越界
    }

    pub fn format() {
        let s1 = String::from("tic");
        let s2 = String::from("tac");
        let s3 = String::from("toe");

        let s = format!("{s1}-{s2}-{s3}");
    }

    pub fn on_for() {
        for c in "Зд".chars() {
            println!("{c}");
        }

        for b in "Зд".bytes() {
            println!("{b}");
        }
    }
}

pub mod ab_map {
    use std::collections::HashMap;
    pub fn base() {
        let mut scores = HashMap::new();
        scores.insert(String::from("Blue"), 10);
        scores.insert(String::from("Yellow"), 50);

        let team_name = String::from("Blue");
        let score = scores.get(&team_name);

        for (key, value) in &scores {
            println!("{key}: {value}");
        }
    }

    pub fn owner() {
        let field_name = String::from("Favorite color");
        let field_value = String::from("Blue");

        let mut map = HashMap::new();
        map.insert(field_name, field_value);
        // 这里 field_name 和 field_value 不再有效，
        // 尝试使用它们看看会出现什么编译错误！

        // 这里可以重新声明 field_name 和 field_value，
        // 因为它们的作用域已经结束。
    }

    pub fn update() {
        let mut scores = HashMap::new();
        scores.insert(String::from("Blue"), 10);

        scores.entry(String::from("Yellow")).or_insert(50);
        scores.entry(String::from("Blue")).or_insert(50);

        println!("{:?}", scores);

        let text = "hello world wonderful world";

        let mut map = HashMap::new();

        for word in text.split_whitespace() {
            let count = map.entry(word).or_insert(0);
            *count += 1;
        }

        println!("{:?}", map);
    }
}
